﻿Imports VendasBLL
Imports VendasDAL
Public Class PlacaFRM
    Dim ObjBLL As New PlacaBLL
    Public Op As String
    Public CodCarro As Integer
    Public CodCliente As Integer
    Public Codigo As Integer
#Region "Carrega Campos"
    Sub CarregaCampos()
        ObjBLL = New PlacaBLL(codigo)
        LBCodigo.Text = "Código: " & ObjBLL.Codigo
        LBTitulo.Text = ObjBLL.Descricao
        CodCarro = ObjBLL.CarCodigo
        CodCliente = ObjBLL.CliCodigo
        TXTCarro.Text = ObjBLL.CarDescricao
        TXTPlaca.Text = ObjBLL.Descricao
        TXTCliente.Text = ObjBLL.CliDescricao
        TXTChassi.Text = ObjBLL.Chassi
        Dim Data As DateTime = "01/01/" & ObjBLL.Ano.ToString
        TXTAno.Value = Data
        CBStatus.SelectedValue = ObjBLL.Status
    End Sub
#End Region

#Region "Limpar campos"
    Sub Limpa()

        CBStatus.SelectedValue = 1
        TXTPlaca.Text = ""
        TXTCarro.Text = ""
        CodCarro = 0
        CodCliente = 0
        TXTCliente.Text = ""
        TXTChassi.Text = ""
        TXTAno.Text = Nothing
        TXTCor.Text = ""
        Me.ObjBLL = Nothing
    End Sub
#End Region

    Sub Resolucao()

        Dim intX As Integer = Screen.PrimaryScreen.Bounds.Width
        Dim intY As Integer = Screen.PrimaryScreen.Bounds.Height
        If (intX & intY) = "19201080" Then
            Me.Font = New Font("Verdana", 12)
            PTopo.Font = New Font("Verdana", 12)
            TSMenu.Font = New Font("Verdana", 12)
        End If
    End Sub
#Region "Load"
    Private Sub PlacaFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        '     Resolucao()
        objconexao.carregaCombo(CBStatus, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo < 3", "sta_codigo")
        Limpa()
        If (Op = "I") Then
            LBCodigo.Visible = False
            LBTitulo.Text = "Placa"
            LBCodigo.Text = ""
        Else
            LBCodigo.Visible = True
            CarregaCampos()
        End If
    End Sub
#End Region

#Region "Salvar/Alterar"
    Private Sub TSBSalvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSalvar.Click
        Try
            ObjBLL = New PlacaBLL(Codigo, Trim(TXTPlaca.Text), CBStatus.SelectedValue, Trim(TXTAno.Text), Trim(TXTCor.Text), Trim(TXTChassi.Text), CodCliente, CodCarro)
            If Op = "I" Then
                ObjBLL.Inserir()
                MessageBox.Show("Placa inserido com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Limpa()
                Me.Close()
            Else
                ObjBLL.Alterar()
                MessageBox.Show("Placa alterada com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Limpa()
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region

    Private Sub TXTAno_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub


    Private Sub BCliente_Click(sender As System.Object, e As System.EventArgs) Handles BCliente.Click
        Dim ObjCC As New ConsultaClientesFRM
        ObjCC.tipo = "ret"
        ObjCC.ShowDialog()
        If (Not IsNothing(ObjCC.ObjC)) Then
            Dim ObjCliente As New ClientesBLL
            ObjCliente = ObjCC.ObjC
            TXTCliente.Text = ObjCliente.Nome
            CodCliente = ObjCliente.Codigo
            ObjCliente = Nothing
        End If
    End Sub

    Private Sub BCarro_Click(sender As System.Object, e As System.EventArgs) Handles BCarro.Click
        Dim Obj As New ConsultaCarrosFRM
        Obj.tipo = "ret"
        Obj.ShowDialog()
        If (Not IsNothing(Obj.ObjC)) Then
            Dim ObjCarro As New CarroBLL
            ObjCarro = Obj.ObjC
            TXTCarro.Text = ObjCarro.Descricao
            CodCarro = ObjCarro.Codigo
            ObjCarro = Nothing
        End If
    End Sub


    Private Sub PlacaFRM_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If
    End Sub

    Private Sub TXTPlaca_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles TXTPlaca.Validating
        TXTPlaca.Text = TXTPlaca.Text.ToUpper
    End Sub
End Class